package com.service;

import com.alibaba.fastjson.JSON;
import com.entity.MenDian_xtl;
import com.utils.EntityFactory;
import com.utils.ExecUtil;
import com.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = {"/guanli_query","/guanli_add","/guanli_delete"})
public class GuanLiService_db extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!req.getServletPath().equals("/guanli_query")) {
            resp.getWriter().write(Result.fail(404, "网址错误"));
            return;
        }
        String sql = "select m.* from mendian_xtl m inner join xiaoqu_mendian_db xm on m.id = xm.m_id and xm.x_id = ?";
        List<MenDian_xtl> menDians = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        params.add(req.getParameter("id"));
        if (!req.getParameter("name").isEmpty()) {
            params.add(req.getParameter("name"));
            sql = sql + " where name like \"%\"?\"%\"";
        }
        try {
            ResultSet resultSet = (ResultSet) ExecUtil.execute(sql, params, req, false);
            while (resultSet.next()) {
                EntityFactory entityFactory = new EntityFactory(MenDian_xtl.class);
                MenDian_xtl menDian = (MenDian_xtl) entityFactory.startCombine(resultSet);
                menDians.add(menDian);
            }
            resp.getWriter().write(JSON.toJSONString(menDians));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sql;
        List<Object> params = new ArrayList<>();
        String data;
        if (!req.getServletPath().equals("/guanli_add") && !req.getServletPath().equals("/guanli_delete")) {
            resp.getWriter().write(Result.fail(404, "网址错误"));
            return;
        }
        if (req.getServletPath().equals("/guanli_add")) {
            sql = "insert into xiaoqu_mendian_db(x_id,m_id) values(?,?)";
            params.add(req.getParameter("id1"));
            params.add(req.getParameter("id2"));
            data = "添加成功";
        } else {
            sql = "delete from xiaoqu_mendian_db where x_id = ? and m_id = ?";
            params.add(req.getParameter("id1"));
            params.add(req.getParameter("id2"));
            data = "删除成功";
        }
        try {
            ExecUtil.execute(sql, params, req, true);
            resp.getWriter().write(Result.success(data));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
